package com.swift.maintenance.utils;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.swift.maintenance.R;

public class ToastUtil {

	volatile static ToastUtil instances;
	final int SHOW_TIME = 2000;
	Context context;
	Toast toast;
	LayoutInflater localinflater;

	public static ToastUtil getInstances() {
		if (null == instances) {
			synchronized (ToastUtil.class) {
				if (null == instances) {
					instances = new ToastUtil();
				}
			}
		}
		return instances;
	}

	public void init(Context context) {
		this.context = context;
		localinflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	Runnable buz = new Runnable() {

		@Override
		public void run() {
			toast.cancel();
		}
	};

	public void showMsg(String msg) {
		//移除取消的事件，每次都重新设置取消的时间
		mHandler.removeCallbacks(buz);
		
		//获取自定义布局
		View view = localinflater.inflate(R.layout.toast_tip, null, false);
		TextView tvTip = (TextView) view.findViewById(R.id.tvTip);
		//设置提示文本
		tvTip.setText(msg);
		//初始化TOAST
		if (null == toast) {
			toast = new Toast(context);
		}
		toast.setDuration(Toast.LENGTH_SHORT);
		toast.setView(view);
		toast.show();
		//发起一个取消事件
		mHandler.postDelayed(buz, SHOW_TIME);
	}
	
	public void showMsg(int msgRec) {
		String msg = context.getResources().getString(msgRec);
		showMsg(msg);
	}

	Handler mHandler = new Handler() {

	};
}
